<?php
class UsuariosFinalesController extends CustomController
{

    public function listarUsuariosFinales()
    {
        // Check login
        $this->userLoggedIn();
        $this->isAdmin();
        // Listar
        if ($_POST || (!empty($_GET['filter'])))
        {
            // Get filter
            $filter = $this->getValue('filter');
            $this->sanitizeFilter($filter);
            // Filter Url for pagination
            $filterUrl = '';
            $filterSymbol = '?';
            foreach($filter as $k => $v)
            {
                $filterUrl .= $filterSymbol."filter[$k]=$v";
                $filterSymbol = '&';
            }
            // Page num
            $pageNumber = $this->getPage();
            // Get results
            $results = $this->model->getUsuariosFinales($filter, $pageNumber);
            if (empty($results))
            {
                $this->error('No se obtuvieron resultados para la b&uacute;squeda.');
            }
        }
        else
        {
            $filter = array(
                'per_page' => 10,
            );
            $results = false;
            $filterUrl = '';
            $pageNumber = 0;
        }
        $vars = array(
            'delete_link'     => $this->link(MODULE_USUARIOS_FINALES, MODULE_USUARIOS_FINALES_BORRAR),
            'delete_keep_url' => base64_encode($filterUrl.'&page='.$pageNumber),
            'search_url'      => $this->link(MODULE_USUARIOS_FINALES, MODULE_USUARIOS_FINALES_LISTAR),
            'pagination_link' => $this->link(MODULE_USUARIOS_FINALES, MODULE_USUARIOS_FINALES_LISTAR).$filterUrl,
            'filter'          => $filter,
            'results'         => $results
        );
        $this->view->listarUsuariosFinales($vars);
    }

    public function borrarUsuariosFinales()
    {
        // Check login
        $this->userLoggedIn();
        $this->isAdmin();
        // Listar
        $users = $this->getValue('users');
        if (!empty($users))
        {
            if ($this->model->borrarUsuariosFinales($users))
            {
                $this->notice('Los usuarios han sido borrados exitosamente');
            }
            else
            {
                $this->error('Error borrando usuarios');
            }
        }
        else
        {
            $this->error('Error borrando usuarios');
        }
        $this->redirectTo(MODULE_USUARIOS_FINALES, MODULE_USUARIOS_FINALES_LISTAR, base64_decode($this->getValue('delete_keep_url')));
    }

    private function sanitizeFilter(&$filter)
    {
        // Register date
        if (!empty($filter['register_to']) && !empty($filter['register_from']))
        {
            $filter['register_from_ts'] = strtotime($filter['register_from'].' 00:00:00');
            $filter['register_to_ts']   = strtotime($filter['register_to'].' 23:59:59');
        }
        else
        {
            $filter['register_from'] = '';
            $filter['register_to'] = '';
        }
        // Last login date
        if (!empty($filter['login_to']) && !empty($filter['login_from']))
        {
            $filter['login_from_ts'] = strtotime($filter['login_from'].' 00:00:00');
            $filter['login_to_ts']   = strtotime($filter['login_to'].' 23:59:59');
        }
        else
        {
            $filter['login_from'] = '';
            $filter['login_to']   = '';
        }
        // Email
        if (!validEmail($filter['email']))
        {
            $filter['email'] = '';
        }
        // Results per page
        if (!empty($filter['per_page']))
        {
            if ($filter['per_page'] != 'max')
            {
                $filter['per_page'] = intval($filter['per_page']);
                switch ($filter['per_page'])
                {
                    case 10  :
                    case 20  :
                    case 50  :
                    case 100 :
                        $filter['per_page'] = $filter['per_page'];
                        break;
                    default :
                        $filter['per_page'] = 10;
                    break;
                }
            }
        }
    }

}
?>